Skip to content

8365192: post_meth_exit should be in vm state when calling get_jvmti_thread_state #26713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

lmesnik
Copy link
Member

@lmesnik lmesnik commented Aug 9, 2025

The method
get_jvmti_thread_state()
should be called only while thread is in vm state.

The post_method_exit is doing some preparation before switching to vm state. This cause issues if thread is needed to initialize jvmti thread state.

The fix was found using jvmti stress agent and thus no additional regression test is required.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8365192: post_meth_exit should be in vm state when calling get_jvmti_thread_state (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26713/head:pull/26713
$ git checkout pull/26713

Update a local copy of the PR:
$ git checkout pull/26713
$ git pull https://git.openjdk.org/jdk.git pull/26713/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26713

View PR using the GUI difftool:
$ git pr show -t 26713

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26713.diff

Using Webrev

Link to Webrev Comment

@lmesnik lmesnik marked this pull request as ready for review August 9, 2025 20:30
@bridgekeeper
Copy link

bridgekeeper bot commented Aug 9, 2025

👋 Welcome back lmesnik! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 9, 2025

@lmesnik This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8365192: post_meth_exit should be in vm state when calling get_jvmti_thread_state

Reviewed-by: sspitsyn

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 48 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Aug 9, 2025

@lmesnik The following labels will be automatically applied to this pull request:

  • hotspot
  • serviceability

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Aug 9, 2025

Webrevs

Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching and addressing this! How was the fix tested?
It looks okay at a glance but may give surprises.

@lmesnik
Copy link
Member Author

lmesnik commented Aug 12, 2025

I ran tier1-5 testing and separately verified that test pass with jvmti strass agent.

JavaThread* current = thread; // for JRT_BLOCK
JRT_BLOCK
state = get_jvmti_thread_state(thread);
JRT_BLOCK_END
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JRT_BLOCK is defined as:

#define JRT_BLOCK                                                    \
    {                                                                \
    assert(current == JavaThread::current(), "Must be");             \
    ThreadInVMfromJava __tiv(current);                               \
    JavaThread* THREAD = current; /* For exception macros. */        \
    DEBUG_ONLY(VMEntryWrapper __vew;)

I'd suggest something like this instead of using JRT_BLOCK:

-  JvmtiThreadState *state = get_jvmti_thread_state(thread);
+   JvmtiThreadState *state = nullptr;
+  {
+     ThreadInVMfromJava __tiv(thread);
+     state = get_jvmti_thread_state(thread);
+  }

Alternatively, the JRT_BLOCK can be started at the line 1837 and ended with JRT_BLOCK_END at the line 1875. Not sure, what issue we can encounter with this though. At least, it is worth a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand the post_method_entry was called via JRT_BLOCK_ENTRY and not JRT_BLOCK by the reason. We need to be in Java. See comments for the method invocation.

// This is a JRT_BLOCK_ENTRY because we have to stash away the return oop
// before transitioning to VM, and restore it after transitioning back
// to Java. The return oop at the top-of-stack, is not walked by the GC.
JRT_BLOCK_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread* current))
  LastFrameAccessor last_frame(current);
  JvmtiExport::post_method_exit(current, last_frame.method(), last_frame.get_frame());
JRT_END

And thanks for simplification, it is a good idea. I've updated the PR.
I am running tier1-8 for Hotspot tests to ensure that nothing is broken.

@sspitsyn
Copy link
Contributor

I ran tier1-5 testing and separately verified that test pass with jvmti strass agent.

Okay, thanks. I'd also run the tier 6 to be more safe.

Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good


JvmtiThreadState *state = nullptr;
{
ThreadInVMfromJava __tiv(thread);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe rename: __tiv => tiv. The prefix __ is normally used in macros to avoid potential naming conflicts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the update on this!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 13, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Aug 13, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 13, 2025
JvmtiThreadState *state = nullptr;
{
ThreadInVMfromJava tiv(thread);
state = get_jvmti_thread_state(thread);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I see is that get_jvmti_thread_state() can safepoint for virtual threads (and now also for platform threads because of ~ThreadInVMfromJava), which brings us back to the bug 8255452 was trying to fix: if there is a return oop at the top of the stack, it could become invalid if a GC occurs. I think we will have to unconditionally save the return value in case it's an oop, before doing anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review serviceability [email protected]
Development

Successfully merging this pull request may close these issues.

3 participants